/**
 * @author banglong
 * @date 2016/9/2.
 */
/**
 * @author banglong
 * @date 2016/8/24.
 */
define(['Condition', 'Conditions'], function (Condition, Conditions) {

  // get Conditions
  var getDrawBillConditions = function (param) {
    var startTime = param.startTime; // 开始时间
    var endTime = param.endTime; // 结束时间
    var BillType = param.BillType; // 单据类型 领用，转移，消耗。。。
    var GoodsBookID = param.GoodsBookID; // 账册
    var OutDeptAccountID = param.OutDeptAccountID; // 出库部门
    var Status = param.Status || null; // 单据状态
    var InDeptAccountID = param.InDeptAccountID; // 进库部门 （本部门）
    var AdditionalReasonType = param.AdditionalReasonType; // 来源类型

    var conditions = [];
    var outDepCondition = new Condition("OutDeptAccountID", null, "Equal", OutDeptAccountID, "String");
    var inDeptCondition;
    if (!InDeptAccountID) { // 当出库科室账号和登录科室账号一样的时候 不做 判断
      inDeptCondition = new Condition("InDeptAccountID", null, "NotEqual", null, "String");
    } else {
      inDeptCondition = new Condition("InDeptAccountID", null, "Equal", InDeptAccountID, "String");
    }

    var oneConditions = new Conditions([outDepCondition, inDeptCondition], "AND");
    conditions.push(oneConditions);

    var statusCondition;
    if (param.isProviderDept) { // 当出库科室账号和登录科室账号一样的时候 不能查到已录入的状态
      if (Status === null) {
        statusCondition = new Condition("Status", null, "NotEqual", 'lr', "String");
      } else if (Status === 'lr') {
        statusCondition = new Condition("Status", null, "Equal", 'null', "String");
      } else if (Status !== null && Status !== 'lr') {
        statusCondition = new Condition("Status", null, "Equal", Status, "String");
      }
    } else {
      statusCondition = new Condition("Status", null, (Status === null ? "NotEqual" : "Equal"), Status, "String");
    }
    conditions.push(statusCondition);

    if (startTime && !param.IsQuery) {
      var StartTimeCondition = new Condition('InputTime', null, 'MoreOrEqual', startTime, "DateTime");
      conditions.push(StartTimeCondition);
    }

    if (endTime && !param.IsQuery) {
      var EndTimeCondition = new Condition('InputTime', null, 'LessOrEqual', endTime, "DateTime");
      conditions.push(EndTimeCondition);
    }

    var BillTypeCondition = new Condition('BillType', null, 'Equal', BillType, "String");
    conditions.push(BillTypeCondition);
    var GoodsBookIDCondition = new Condition('GoodsBookID', null, 'Equal', GoodsBookID, "String");
    conditions.push(GoodsBookIDCondition);

    if (AdditionalReasonType !== 'null' && !param.IsQuery) {
      var AdditionalReasonTypeCondition = new Condition('AdditionalReasonType', null, "Equal", AdditionalReasonType, "String");
      conditions.push(AdditionalReasonTypeCondition);
    }

    if (param.sendStartDate && param.IsQuery) {
      var SendStartDate = new Condition('SendTime', null, 'MoreOrEqual', param.sendStartDate, "DateTime");
      conditions.push(SendStartDate);
    }
    if (param.sendEndDate && param.IsQuery) {
      var SendEndDate = new Condition('SendTime', null, 'LessOrEqual', param.sendEndDate, "DateTime");
      conditions.push(SendEndDate);
    }
    if (param.outStartDate && param.IsQuery) {
      var OutStartDate = new Condition('DiliverTime', null, 'MoreOrEqual', param.outStartDate, "DateTime");
      conditions.push(OutStartDate);
    }
    if (param.outEndDate && param.IsQuery) {
      var OutEndDate = new Condition('DiliverTime', null, 'LessOrEqual', param.outEndDate, "DateTime");
      conditions.push(OutEndDate);
    }
    if (param.BillSN && param.IsQuery) {
      var BillSN = new Condition('BillSN', null, 'Equal', param.BillSN, "AnsiString");
      conditions.push(BillSN);
    }

    return new Conditions(conditions, "AND");
  };

  var getTransferBillConditions = function (param) {
    var startTime = param.startTime; // 开始时间
    var endTime = param.endTime; // 结束时间
    var BillType = param.BillType; // 单据类型 领用，转移，缴回。。。
    var GoodsBookID = param.GoodsBookID; // 账册
    var OutDeptAccountID = param.OutDeptAccountID; // 出库部门
    var Status = param.Status || null; // 单据状态
    var InDeptAccountID = param.InDeptAccountID; // 进库部门 （本部门）
    var AdditionalReasonType = param.AdditionalReasonType; // 来源类型

    // 转移单 出库科室为本部门
    var outDepCondition;
    var inDeptCondition;
    var statusCondition;

    outDepCondition = new Condition("OutDeptAccountID", null, "Equal", InDeptAccountID, "String");

    if (param.isProviderDept) {

      if (InDeptAccountID) {
        inDeptCondition = new Condition("InDeptAccountID", null, "Equal", InDeptAccountID, "String");
      } else {
        inDeptCondition = new Condition("InDeptAccountID", null, "NotEqual", null, "String");
      }

      if (Status === null) {
        statusCondition = new Condition("Status", null, "NotEqual", 'lr', "String");
      } else if (Status === 'lr') {
        statusCondition = new Condition("Status", null, "Equal", 'null', "String");
      } else if (Status !== null && Status !== 'lr') {
        statusCondition = new Condition("Status", null, "Equal", Status, "String");
      }
    } else {
      statusCondition = new Condition("Status", null, (Status === null ? "NotEqual" : "Equal"), Status, "String");
    }

    var BillTypeCondition = new Condition('BillType', null, 'Equal', BillType, "String");
    var GoodsBookIDCondition = new Condition('GoodsBookID', null, 'Equal', GoodsBookID, "String");

    var conditions = [
      BillTypeCondition,
      GoodsBookIDCondition,
      statusCondition
    ];

    if (AdditionalReasonType !== 'null' && !param.IsQuery) {
      var AdditionalReasonTypeCondition = new Condition('AdditionalReasonType', null, (AdditionalReasonType === null ? "NotEqual" : "Equal"), AdditionalReasonType, "String");
      conditions.push(AdditionalReasonTypeCondition);
    }

    if (!param.isProviderDept) {
      var sh = new Condition("Status", null, 'Equal', 'sh', "String");
      var rk = new Condition("Status", null, 'Equal', 'rk', "String");
      var jz = new Condition("Status", null, 'Equal', 'jz', "String");
      conditions.push(new Conditions([outDepCondition, new Conditions([new Condition("InDeptAccountID", null, 'Equal', InDeptAccountID, "String"), new Conditions([sh, rk, jz], 'OR')], 'AND')], 'OR'))
    } else {
      conditions.push(inDeptCondition);
    }

    if (startTime && !param.IsQuery) {
      var StartTimeCondition = new Condition('InputTime', null, 'MoreOrEqual', startTime, "DateTime");
      conditions.push(StartTimeCondition);
    }

    if (endTime && !param.IsQuery) {
      var EndTimeCondition = new Condition('InputTime', null, 'LessOrEqual', endTime, "DateTime");
      conditions.push(EndTimeCondition);
    }

    if (param.sendStartDate && param.IsQuery) {
      var SendStartDate = new Condition('SendTime', null, 'MoreOrEqual', param.sendStartDate, "DateTime");
      conditions.push(SendStartDate);
    }
    if (param.sendEndDate && param.IsQuery) {
      var SendEndDate = new Condition('SendTime', null, 'LessOrEqual', param.sendEndDate, "DateTime");
      conditions.push(SendEndDate);
    }
    if (param.outStartDate && param.IsQuery) {
      var OutStartDate = new Condition('DiliverTime', null, 'MoreOrEqual', param.outStartDate, "DateTime");
      conditions.push(OutStartDate);
    }
    if (param.outEndDate && param.IsQuery) {
      var OutEndDate = new Condition('DiliverTime', null, 'LessOrEqual', param.outEndDate, "DateTime");
      conditions.push(OutEndDate);
    }
    if (param.BillSN && param.IsQuery) {
      var BillSN = new Condition('BillSN', null, 'Equal', param.BillSN, "AnsiString");
      conditions.push(BillSN);
    }

    return new Conditions(conditions, "AND");
  };

  var getDrawBackBillConditions = function (param) {
    var startTime = param.startTime; // 开始时间
    var endTime = param.endTime; // 结束时间
    var BillType = param.BillType; // 单据类型 领用，转移，消耗。。。
    var GoodsBookID = param.GoodsBookID; // 账册
    var OutDeptAccountID = param.OutDeptAccountID; // 出库部门
    var Status = param.Status || null; // 单据状态
    var InDeptAccountID = param.InDeptAccountID; // 进库部门 （本部门）
    var AdditionalReasonType = param.AdditionalReasonType; // 来源类型

    var outDepCondition;
    var inDeptCondition;

    if (param.isProviderDept) {
      if (!InDeptAccountID) {
        outDepCondition = new Condition("OutDeptAccountID", null, "NotEqual", null, "String");
      } else {
        outDepCondition = new Condition("OutDeptAccountID", null, "Equal", InDeptAccountID, "String");
      }
      inDeptCondition = new Condition("InDeptAccountID", null, "Equal", OutDeptAccountID, "String");
    } else {
      outDepCondition = new Condition("OutDeptAccountID", null, "Equal", InDeptAccountID, "String");
      inDeptCondition = new Condition("InDeptAccountID", null, "NotEqual", null, "String");
    }

    var oneConditions = new Conditions([outDepCondition, inDeptCondition], "AND");
    var statusCondition;
    statusCondition = new Condition("Status", null, (Status === null ? "NotEqual" : "Equal"), Status, "String");

    var BillTypeCondition = new Condition('BillType', null, 'Equal', BillType, "String");
    var GoodsBookIDCondition = new Condition('GoodsBookID', null, 'Equal', GoodsBookID, "String");

    var conditions = [
      BillTypeCondition,
      GoodsBookIDCondition,
      statusCondition,
      oneConditions
    ];

    if (AdditionalReasonType !== 'null' && !param.IsQuery) {
      var AdditionalReasonTypeCondition = new Condition('AdditionalReasonType', null, (AdditionalReasonType === null ? "NotEqual" : "Equal"), AdditionalReasonType, "String");
      conditions.push(AdditionalReasonTypeCondition);
    }

    if (startTime && !param.IsQuery) {
      var StartTimeCondition = new Condition('InputTime', null, 'MoreOrEqual', startTime, "DateTime");
      conditions.push(StartTimeCondition);
    }

    if (endTime && !param.IsQuery) {
      var EndTimeCondition = new Condition('InputTime', null, 'LessOrEqual', endTime, "DateTime");
      conditions.push(EndTimeCondition);
    }

    if (param.sendStartDate && param.IsQuery) {
      var SendStartDate = new Condition('SendTime', null, 'MoreOrEqual', param.sendStartDate, "DateTime");
      conditions.push(SendStartDate);
    }
    if (param.sendEndDate && param.IsQuery) {
      var SendEndDate = new Condition('SendTime', null, 'LessOrEqual', param.sendEndDate, "DateTime");
      conditions.push(SendEndDate);
    }
    if (param.outStartDate && param.IsQuery) {
      var OutStartDate = new Condition('DiliverTime', null, 'MoreOrEqual', param.outStartDate, "DateTime");
      conditions.push(OutStartDate);
    }
    if (param.outEndDate && param.IsQuery) {
      var OutEndDate = new Condition('DiliverTime', null, 'LessOrEqual', param.outEndDate, "DateTime");
      conditions.push(OutEndDate);
    }
    if (param.BillSN && param.IsQuery) {
      var BillSN = new Condition('BillSN', null, 'Equal', param.BillSN, "AnsiString");
      conditions.push(BillSN);
    }

    return new Conditions(conditions, "AND");
  };


  var getBillHeads = function (param) {
    var startTime = param.startTime; // 开始时间
    var endTime = param.endTime; // 结束时间
    var BigCatID = param.BigCatID; // 单据类型 领用，转移，消耗。。。
    var InDeptAccountID = param.InDeptAccountID; // 进库部门 （本部门）
    var Status = param.Status || null;

    var StartTimeCondition = new Condition('InputTime', null, 'MoreOrEqual', startTime, "DateTime");
    var EndTimeCondition = new Condition('InputTime', null, 'LessOrEqual', endTime, "DateTime");
    var BigCatIDCondition = new Condition("BigCatID", null, "Equal", BigCatID, "String");
    var inDeptCondition = new Condition("InDeptAccountID", null, "Equal", InDeptAccountID, "String");
    var StatusCondition;
    if (param.isProviderDept) {
      if (Status === null) {
        StatusCondition = new Condition("Status", null, "NotEqual", 'lr', "String");
      } else if (Status === 'lr') {
        StatusCondition = new Condition("Status", null, "Equal", 'null', "String");
      } else if (Status !== null && Status !== 'lr') {
        StatusCondition = new Condition("Status", null, "Equal", Status, "String");
      }
    } else {
      StatusCondition = new Condition("Status", null, (Status === null ? "NotEqual" : "Equal"), Status, "String");
    }

    var conditions = [inDeptCondition, StartTimeCondition, EndTimeCondition, StatusCondition, BigCatIDCondition];
    if (param.outStartDate && param.IsQuery) {
      var OutStartDate = new Condition('DiliverTime', null, 'MoreOrEqual', param.outStartDate, "DateTime");
      conditions.push(OutStartDate);
    }
    if (param.outEndDate && param.IsQuery) {
      var OutEndDate = new Condition('DiliverTime', null, 'LessOrEqual', param.outEndDate, "DateTime");
      conditions.push(OutEndDate);
    }
    if (param.BillSN && param.IsQuery) {
      var BillSN = new Condition('BillSN', null, 'Equal', param.BillSN, "AnsiString");
      conditions.push(BillSN);
    }
    if (param.isProviderDept && !(InDeptAccountID != param.OutDeptAccountID)) {
      conditions.splice(0, 1);
    }
    return new Conditions(conditions, "AND");
  };

  // 供应室 调换单
  var getTransBillHeads = function (param) {
    var startTime = param.startTime; // 开始时间
    var endTime = param.endTime; // 结束时间
    var BigCatID = param.BigCatID; // 单据类型 领用，转移，消耗。。。
    var InDeptAccountID = param.InDeptAccountID; // 进库部门 （本部门）
    var Status = param.Status || null;

    var StartTimeCondition = new Condition('dj.InputTime', null, 'MoreOrEqual', startTime, "DateTime");
    var EndTimeCondition = new Condition('dj.InputTime', null, 'LessOrEqual', endTime, "DateTime");
    var BigCatIDCondition = new Condition("dj.BigCatID", null, "Equal", BigCatID, "String");
    var inDeptCondition = new Condition("dj.ApplyDeptAccountID", null, "Equal", InDeptAccountID, "String");
    var StatusCondition;
    if (param.isProviderDept) {
      if (Status === null) {
        StatusCondition = new Condition("dj.Status", null, "NotEqual", 'lr', "String");
      } else if (Status === 'lr') {
        StatusCondition = new Condition("dj.Status", null, "Equal", 'null', "String");
      } else if (Status !== null && Status !== 'lr') {
        StatusCondition = new Condition("dj.Status", null, "Equal", Status, "String");
      }
    } else {
      StatusCondition = new Condition("dj.Status", null, (Status === null ? "NotEqual" : "Equal"), Status, "String");
    }
    var BillTypeCondition = new Condition('dj.BillType', null, 'Equal', param.BillType, "String");
    var outDepCondition = new Condition("dj.OutDeptAccountID", null, "Equal", param.OutDeptAccountID, "String");
    var conditions = [inDeptCondition, StartTimeCondition, EndTimeCondition,
      StatusCondition, BigCatIDCondition, BillTypeCondition, outDepCondition
    ];

    if (param.isProviderDept && !InDeptAccountID && InDeptAccountID != param.OutDeptAccountID) {
      conditions.splice(0, 1);
    }

    if (param.GroupSN) {
      conditions.push(new Condition("ksfzmx.GroupSN", null, "Equal", param.GroupSN, "String"));
    }
    return new Conditions(conditions, "AND");
  };

  // 器械消毒单
  var getDisinfectBillHeads = function (param) {
    var startTime = param.startTime; // 开始时间
    var endTime = param.endTime; // 结束时间
    var BigCatID = param.BigCatID; // 单据类型 领用，转移，消耗。。。
    var DeptID = param.DeptID;
    var Status = param.Status || null;
    var BillSN = param.BillSN || null;

    var StartTimeCondition = new Condition('a.InputTime', null, 'MoreOrEqual', startTime, "DateTime");
    var EndTimeCondition = new Condition('a.InputTime', null, 'LessOrEqual', endTime, "DateTime");
    var DeptCondition = new Condition('a.DeptID', null, 'Equal', DeptID, "String");
    var BigCatIDCondition = new Condition("a.BigCatID", null, "Equal", BigCatID, "String");
    var StatusCondition;
    if (param.isProviderDept) {
      if (Status === null) {
        StatusCondition = new Condition("a.Status", null, "NotEqual", 'lr', "String");
      } else if (Status === 'lr') {
        StatusCondition = new Condition("a.Status", null, "Equal", 'null', "String");
      } else if (Status !== null && Status !== 'lr') {
        StatusCondition = new Condition("a.Status", null, "Equal", Status, "String");
      }
    } else {
      StatusCondition = new Condition("a.Status", null, (Status === null ? "NotEqual" : "Equal"), Status, "String");
    }
    var conditions = [StartTimeCondition, EndTimeCondition, DeptCondition, BigCatIDCondition, StatusCondition];
    if (BillSN) {
      var BillSNCondition = new Condition("a.BillSN", null, "Equal", BillSN, "String");
      conditions.push(BillSNCondition);
    }

    if (param.sendStartDate && param.IsQuery) {
      var SendStartDate = new Condition('a.DisinfectTime', null, 'MoreOrEqual', param.sendStartDate, "DateTime");
      conditions.push(SendStartDate);
    }
    if (param.sendEndDate && param.IsQuery) {
      var SendEndDate = new Condition('a.DisinfectTime', null, 'LessOrEqual', param.sendEndDate, "DateTime");
      conditions.push(SendEndDate);
    }
    if (param.outStartDate && param.IsQuery) {
      var OutStartDate = new Condition('a.SendInstrumentTime', null, 'MoreOrEqual', param.outStartDate, "DateTime");
      conditions.push(OutStartDate);
    }
    if (param.outEndDate && param.IsQuery) {
      var OutEndDate = new Condition('a.SendInstrumentTime', null, 'LessOrEqual', param.outEndDate, "DateTime");
      conditions.push(OutEndDate);
    }

    if (param.GroupSN) {
      conditions.push(new Condition("ksfzmx.GroupSN", null, "Equal", param.GroupSN, "String"));
    }
    return new Conditions(conditions, "AND");
  };

  // 设备科 - 申请单
  var getApplyBillHeads = function (param) {
    var StartTimeCondition = new Condition('a.sqsj', null, 'MoreOrEqual', param.startTime, "DateTime");
    var EndTimeCondition = new Condition('a.sqsj', null, 'LessOrEqual', param.endTime, "DateTime");
    var conditions = [StartTimeCondition, EndTimeCondition];

    var StatusCondition;
    if (param.isProviderDept) {
      StatusCondition = new Conditions([new Condition("a.zt", null, "NotEqual", '1', "String"), new Condition("a.zt", null, "NotEqual", '2', "String")], 'AND');
      conditions.push(StatusCondition);
    } else {
      StatusCondition = new Condition("a.zt", null, "NotEqual", null, "String");
      conditions.push(StatusCondition);
      conditions.push(new Condition('a.DeptID', null, 'Equal', param.deptCode, 'String'));
    }
    return new Conditions(conditions, "AND");
  };

  var getChooseConditions = function (param) {
    var conditions;
    switch (param.type) {
      case 0:
        conditions = new Condition("AccountType", null, "Equal", 'zc', "String");
        break;
      case 1:
        conditions = null;
        break;
      case 2:
        conditions = new Condition("zc.GoodsBookID", null, "Equal", param.GoodsBookID, "String");
        break;
      case 3:
        conditions = new Condition("dl.GoodsBigCatID", null, "Equal", param.GoodsBigCatID, "String");
        break;
      case 4:
        conditions = new Condition("zl.GoodsMidCatID", null, "Equal", param.GoodsMidCatID, "String");
        break;
      case 5:
        conditions = null;
        break;
      case 6:
        conditions = null;
        break;
    }
    return conditions;
  };

  var getGoodsCondition = function (param) {
    var IsValid = new Condition("wp.IsValid", null, "Equal", true, "Boolean");
    var IsOpen = new Condition("wp.IsOpen", null, "Equal", (param.IsOpen || false), "Boolean");
    var IsImmediatelyUse = new Condition("wp.IsImmediatelyUse", null, "Equal", (param.IsImmediatelyUse || false), "Boolean");
    var conditions = [IsValid, IsOpen, IsImmediatelyUse];
    // 申请单需要做特殊处理
    if (param.BillType && param.BillType === 'sq') {
      conditions = [IsOpen];
    }
    if (param.isProviderDept) {
      conditions.splice(2, 1);
    }
    if (param.GoodsBookID) {
      var GoodsBookID = new Condition("wp.GoodsBookID", null, "Equal", param.GoodsBookID, "String");
      if (param.isGetAll) {
        GoodsBookID = new Condition("wp.GoodsBookID", null, "In", param.GoodsBookID, "String");
      }
      conditions.push(GoodsBookID)
    }
    if (param.searchValue) {
      var PyCode = new Condition("Upper(wp.PyCode)", null, "Like", '%' + param.searchValue.toLocaleUpperCase() + '%', "String");
      var GoodsCode = new Condition("Upper(GoodsCode)", null, "Like", '%' + param.searchValue.toLocaleUpperCase() + '%', "String");
      var GoodsName = new Condition("GoodsName", null, "Like", '%' + param.searchValue + '%', "String");
      var GoodsSpec = new Condition("Upper(GoodsSpec)", null, "Like", '%' + param.searchValue.toLocaleUpperCase() + '%', "String");
      var orConditions = [PyCode, GoodsCode, GoodsName, GoodsSpec];
      conditions.push(new Conditions(orConditions, 'OR'));
    }
    return new Conditions(conditions, "AND");
  };

  // 包查询的 sql语句
  var getPackageCondition = function (param) {
    var IsValid = new Condition("b.IsValid", null, "Equal", true, "Boolean");
    var conditions = [IsValid];
    if (param.PackageBigCat) {
      var PackageBigCatID = new Condition('b.PackageBigCatID', null, "Equal", param.PackageBigCat, "String");
      conditions.push(PackageBigCatID);
    }

    if (param.searchValue) {
      var PyCode = new Condition("Upper(b.PyCode)", null, "Like", '%' + param.searchValue.toLocaleUpperCase() + '%', "String");
      var GoodsCode = new Condition("Upper(PackageCode)", null, "Like", '%' + param.searchValue.toLocaleUpperCase() + '%', "String");
      var GoodsName = new Condition("PackageName", null, "Like", '%' + param.searchValue + '%', "String");
      var orConditions = [PyCode, GoodsCode, GoodsName];
      conditions.push(new Conditions(orConditions, 'OR'));
    }
    return new Conditions(conditions, "AND");
  };

  var getCardManagerCondition = function (param) {

    var andConditionArr = [];
    var IsOne = new Condition("1", null, "Equal", 1, "Int32");
    andConditionArr.push(IsOne);
    // 是否作废 默认 有效
    var IsCardsNullified;
    if (param.nullifyStatus === 'yx') {
      IsCardsNullified = new Condition("a.IsCardsNullified", null, "Equal", false, "Boolean");
      andConditionArr.push(IsCardsNullified);
    } else if (param.nullifyStatus === 'zf') {
      IsCardsNullified = new Condition("a.IsCardsNullified", null, "Equal", true, "Boolean");
      andConditionArr.push(IsCardsNullified);
    }

    // 物品状态 默认 正常
    var GoodsStatus;
    if (param.scrapStatus !== 'all') {
      GoodsStatus = new Condition("a.GoodsStatus", null, 'Equal', param.scrapStatus, 'String');
      andConditionArr.push(GoodsStatus);
    }

    var DeptAccountID = new Condition("d.DeptAccountID", null, 'Equal', param.deptAccountID, 'String');
    var DrawDeptAccountID = new Condition("a.DrawDeptAccountID", null, 'Equal', param.deptAccountID, 'String');
    andConditionArr.push(DrawDeptAccountID);
    // 是否赠送
    var IsFree;
    if (param.isGive === 'yse') {
      IsFree = new Condition("a.IsFree", null, 'Equal', true, 'Boolean');
      andConditionArr.push(IsFree);
    } else if (param.isGive === 'no') {
      IsFree = new Condition("a.IsFree", null, 'Equal', false, 'Boolean');
      andConditionArr.push(IsFree);
    }
    // 账册
    if (param.goodsBookID) {
      var GoodsBookID = new Condition("b.GoodsBookID", null, 'Equal', param.goodsBookID, 'String');
      andConditionArr.push(GoodsBookID);
    }
    // 物品id
    if (param.goodsID) {
      var GoodsID = new Condition("a.GoodsID", null, 'Equal', param.goodsID, 'String');
      andConditionArr.push(GoodsID);
    }
    // 作废时间
    if (!!param.finishDisCardTime) {
      var FinishDisCardTime = new Condition("SUBSTRING(CONVERT(VARCHAR(8),a.finishDisCardTime,120),1,4)", null, 'Equal', param.finishDisCardTime, 'String');
      andConditionArr.push(FinishDisCardTime);
    }
    // 购置时间
    if (param.buyStartDate) {
      var BuyStartDate = new Condition("a.PurchaseTime", null, 'MoreOrEqual', param.buyStartDate, 'DateTime');
      andConditionArr.push(BuyStartDate);
    }
    if (param.buyEndDate) {
      var BuyEndDate = new Condition("a.PurchaseTime", null, 'LessOrEqual', param.buyEndDate, 'DateTime');
      andConditionArr.push(BuyEndDate);
    }
    // 卡片号
    if (param.cardNumber) {
      var CardNumber = new Condition("Upper(CardNumber)", null, 'Equal', param.cardNumber, 'String');
      andConditionArr.push(CardNumber);
    }
    // 条形码
    if (param.barcode) {
      var Barcode = new Condition("Upper(BarCode)", null, 'Equal', param.barcode, 'String');
      andConditionArr.push(Barcode);
    }
    // 仪器编码
    if (param.machineCode) {
      var InstrumentCode = new Condition("a.InstrumentCode", null, 'Like', param.machineCode + '%', 'String');
      andConditionArr.push(InstrumentCode);
    }
    //  生产厂商
    if (param.chooseManufacturer.length !== 0) {
      (function () {

        if (param.chooseManufacturer.length === 1) {
          andConditionArr.push(new Condition("a.ManufacturerID", null, 'Equal', param.chooseManufacturer[0].SupplierID, 'String'));
        } else {
          var orConditionArr = [];
          param.chooseManufacturer.forEach(function (data) {
            orConditionArr.push(new Condition("a.ManufacturerID", null, 'Equal', data.SupplierID, 'String'));
          });
          andConditionArr.push(new Conditions(orConditionArr, 'OR'));
        }
      }());
    }
    // 供应厂商
    if (param.chooseSupplier.length !== 0) {
      (function () {

        if (param.chooseSupplier.length === 1) {
          andConditionArr.push(new Condition("a.SupplierID", null, 'Equal', param.chooseSupplier[0].SupplierID, 'String'));
        } else {
          var orConditionArr = [];
          param.chooseSupplier.forEach(function (data) {
            orConditionArr.push(new Condition("a.SupplierID", null, 'Equal', data.SupplierID, 'String'));
          });
          andConditionArr.push(new Conditions(orConditionArr, 'OR'));
        }
      }());
    }
    // 报关时间
    if (param.applyStartDate) {
      var applyStartDate = new Condition("a.Bgsj", null, 'MoreOrEqual', param.applyStartDate, 'DateTime');
      andConditionArr.push(applyStartDate);
    }
    if (param.applyEndDate) {
      var applyEndDate = new Condition("a.Bgsj", null, 'LessOrEqual', param.applyEndDate, 'DateTime');
      andConditionArr.push(applyEndDate);
    }
    // 价格段
    if (param.priceStart) {
      var priceStart = new Condition("a.RmbPrice", null, 'MoreOrEqual', param.priceStart, 'Decimal');
      andConditionArr.push(priceStart)
    }
    if (param.priceEnd) {
      var priceEnd = new Condition("a.RmbPrice", null, 'LessOrEqual', param.priceEnd, 'Decimal');
      andConditionArr.push(priceEnd)
    }
    // 申康编号
    if (param.skNumber) {
      var skNumber = new Condition("Upper(a.Skbh)", null, 'Like', '%' + param.skNumber + '%', 'String');
      andConditionArr.push(skNumber)
    }
    var orCondition = new Conditions([DeptAccountID, DrawDeptAccountID], 'OR');
    var andCondition = new Conditions(andConditionArr, 'AND');
    return new Conditions([orCondition, andCondition], 'AND');
  };

  // 正式库存查询
  var rtstoreQuery = function (param) {
    var addConditions = [];
    if (param.DeptAccountID) {
      var DeptAccountID = new Condition("zskc.DeptAccountID", null, 'Equal', param.DeptAccountID, 'String');
      addConditions.push(DeptAccountID)
    }
    if (param.PackageBigCatID) {
      var PackageBigCatID = new Condition("b.PackageBigCatID", null, 'Equal', param.PackageBigCatID, 'String');
      addConditions.push(PackageBigCatID)
    }
    if (param.PackageID) {
      var PackageID = new Condition("b.PackageID", null, 'Equal', param.PackageID, 'String');
      addConditions.push(PackageID)
    }

    return new Conditions(addConditions, 'AND');
  };

  // 批次查询
  var getFormalPackageBatchLayout = function () {

  };

  // 卡片管理 编码查询
  var selectCode = function (param) {
    var andCondition = [];
    var orCondition = [];

    var IsOpen = new Condition("a.IsOpen", null, "Equal", true, "Boolean");
    andCondition.push(IsOpen);
    var GoodsBookID = new Condition('a.GoodsBookID', null, 'Like', '%' + (param.goodsBookID ? param.goodsBookID : '') + '%', "String");
    andCondition.push(GoodsBookID);

    var PyCode = new Condition('Upper(a.PyCode)', null, 'Like', '%' + (param.searchValue ? param.searchValue.toUpperCase() : '') + '%', 'String');
    var MainInstrumentCode = new Condition('a.MainInstrumentCode', null, 'Like', (param.searchValue ? param.searchValue : '') + '%', 'String');
    orCondition.push(PyCode, MainInstrumentCode);

    var conditions1 = new Conditions(andCondition, 'AND');
    var conditions2 = new Conditions(orCondition, 'OR');
    if (orCondition.length > 0) {
      return new Conditions([conditions1, conditions2], 'AND');
    } else {
      return conditions1;
    }
  };

  return {
    getDrawBillConditions: getDrawBillConditions,
    getTransferBillConditions: getTransferBillConditions,
    getDrawBackBillConditions: getDrawBackBillConditions,
    getBillHeads: getBillHeads,
    getTransBillHeads: getTransBillHeads,
    getDisinfectBillHeads: getDisinfectBillHeads,
    getChooseConditions: getChooseConditions,
    getGoodsCondition: getGoodsCondition,
    getCardManagerCondition: getCardManagerCondition,
    rtstoreQuery: rtstoreQuery,
    getFormalPackageBatchLayout: getFormalPackageBatchLayout,
    selectCode: selectCode,
    getApplyBillHeads: getApplyBillHeads,
    getPackageCondition: getPackageCondition
  };
});